package com.wang.helloandroid.service;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class LocationService implements LocationListener{
	public final static int REQUEST_CODE_LOCATION_SERVICE = 1001;

	boolean providerIsDisabled = false;
	public boolean isDisabled() {
		return providerIsDisabled;
	}

	Context context;
	LocationManager locationManager;
	
	Location location;

	public Location getLocation(int timeout) throws InterruptedException {
		int waitingTime = 0;
		while(null == location && !providerIsDisabled && (waitingTime += 1000) <= timeout)
		{
			Thread.sleep(1000);
		}
		
		return getLocation();
	}

	public Location getLocation() throws InterruptedException {
		return location;
	}

	public void updateProvider(boolean enable)
	{
		if(locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ))
		{
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
		}
		else if(locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER ))
		{
			locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
		}
		else
		{
			providerIsDisabled = true;
			if(enable)
			{
				enableLocationServiceProvider();
			}
		}
	}

	public void enableLocationServiceProvider()
	{
		Builder dialogBuilder = new AlertDialog.Builder(context)
			.setMessage("Yout GPS seems to be disabled, please enable it?")
			.setCancelable(false);
	
		dialogBuilder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener(){
	        public void onClick(DialogInterface dialog, int which) {
	            Intent callGPSSettingIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
	            ((Activity) context).startActivityForResult(callGPSSettingIntent, REQUEST_CODE_LOCATION_SERVICE);
	        }
	    });
	
		dialogBuilder.setNegativeButton("Don't do it", new DialogInterface.OnClickListener(){
	        public void onClick(DialogInterface dialog, int which) {
	        	dialog.cancel();
	        }
	    });
		
		final AlertDialog alert = dialogBuilder.create();
		alert.show();
	}

	public LocationService(Context context, boolean update) {
		this.context = context;
		this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		if(update)
		{
			updateProvider(true);
		}
	}

	@Override
	public void onLocationChanged(Location location) {
		this.location = location;
        Log.i(this.getClass().getName(), "Latitude: " + location.getLatitude() + ", Longitude: " + location.getLongitude());
	}

	@Override
	public void onProviderDisabled(String str) {
		providerIsDisabled = true;
		Log.i(this.getClass().getName(), "GPS service is disabled: " + str);
		
		Toast.makeText(context, "Location service is disabled",Toast.LENGTH_LONG).show();
	}

	@Override
	public void onProviderEnabled(String str) {
		providerIsDisabled = false;
		Log.i(this.getClass().getName(), "Location service is enabled: " + str);
		
		updateProvider(false);
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle bundle) {
		Log.i(this.getClass().getName(), "Status is changed: " + arg1);
	}
}
